package com.xxxy.jsq.action.level;

import com.xxxy.jsq.dao.LevelDao;
import com.xxxy.jsq.model.Level;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class deletelevelServlet
 */
@WebServlet("/deletelevelServlet")
public class deleteLevelServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteLevelServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        Level model = new Level();
        LevelDao dao = new LevelDao();

        // 获取页面参数 cid
        Long cid = null;
        String cidStr = request.getParameter("levelId");
        if (cidStr != null && !cidStr.isEmpty()) {
            try {
                cid = Long.valueOf(cidStr);
            } catch (NumberFormatException e) {
                PrintWriter out = response.getWriter();
                out.print("Err");
                out.flush();
                out.close();
                return;
            }
        }

        if (cid == null) {
            PrintWriter out = response.getWriter();
            out.print("Err");
            out.flush();
            out.close();
            return;
        }

        // 调用 DAO 删除数据
        String flag = dao.deleteById(cid);

        // 返回结果给前端
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
